package com.tjcd.project.library_base.common.utils;

import android.content.Context;
import android.content.res.AssetManager;

import androidx.annotation.Keep;

import com.blankj.utilcode.util.ObjectUtils;
import com.contrarywind.interfaces.IPickerViewData;
import com.google.gson.Gson;

import org.json.JSONArray;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import lombok.Data;


/**
 * @Author jiangKunKun
 * @CreateDate 2021/7/12
 * @Description 城市列表 + code
 * @SpittingGrooves
 */
@Keep
@Data
public class CityUtils {

    /**
     * 省列表
     */
    private List<ProvinceBean> mProvinces = new ArrayList<>();
    /**
     * 区列表
     */
    private List<List<CityBean>> mCitys = new ArrayList<>();
    /**
     * 数据
     */
    private ArrayList<ProvinceBean> mDatas = new ArrayList<>();

    public CityUtils(Context context) {
        // 获取assets目录下的json文件数据
        parseData(getJson(context, "area.json"));
    }

    public List<ProvinceBean> getProvince() {
        if (ObjectUtils.isNotEmpty(mProvinces)) {
            return mProvinces;
        }
        for (int i = 0; i < mDatas.size(); i++) {
            mProvinces.add(mDatas.get(i));
        }
        return mProvinces;
    }

    public List<List<CityBean>> getCity() {
        if (ObjectUtils.isNotEmpty(mCitys)) {
            return mCitys;
        }
        for (int i = 0; i < mDatas.size(); i++) { // 遍历省份
            mCitys.add(new ArrayList<>(mDatas.get(i).getCities()));
        }
        return mCitys;
    }

    public String getJson(Context context, String fileName) {

        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    /**
     * 城市选择器
     * @param result
     * @return
     */
    public ArrayList<ProvinceBean> parseData(String result) {
        try {
            JSONArray data = new JSONArray(result);
            Gson gson = new Gson();
            for (int i = 0; i < data.length(); i++) {
                ProvinceBean entity = gson.fromJson(data.optJSONObject(i).toString(), ProvinceBean.class);
                mDatas.add(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mDatas;
    }

    /**
     * 解析bean
     */
    @Keep
    @Data
    public class ProvinceBean implements IPickerViewData {
        /**
         * pName : 全国
         * cities : []
         * pCode :
         */
        private String pName;
        private String pCode;
        private List<CityBean> cities;

        @Override
        public String getPickerViewText() {
            return pName;
        }

    }

    /**
     * 解析bean
     */
    @Keep
    @Data
    public class CityBean implements IPickerViewData {
        /**
         * cCode : 110100
         * cName : 北京市
         */
        private String cCode;
        private String cName;

        @Override
        public String getPickerViewText() {
            return cName;
        }
    }

}
